/**
 * @class   wosh::security::SecurityAuthentication3rdParty
 * @brief   

 *
 ****************************************************************************
 * @version $Id: SecurityAuthentication3rdParty.h 2732 2010-07-06 03:18:11Z alex $
 * @author  Alessandro Polo
 ****************************************************************************/
/* Copyright (c) 2007-2010, WOSH - Wide Open Smart Home 
 * by Alessandro Polo - OpenSmartHome.com
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *     * Redistributions of source code must retain the above copyright
 *       notice, this list of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above copyright
 *       notice, this list of conditions and the following disclaimer in the
 *       documentation and/or other materials provided with the distribution.
 *     * Neither the name of the OpenSmartHome.com WOSH nor the
 *       names of its contributors may be used to endorse or promote products
 *       derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY Alessandro Polo ''AS IS'' AND ANY
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL Alessandro Polo BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 ****************************************************************************/

#ifndef __WOSH_Security_SecurityAuthentication3rdParty_H__
 #define __WOSH_Security_SecurityAuthentication3rdParty_H__

 #include <woshDefs.h>
 #include <core/Object.h>
 #include <core/SecurityToken.h>


namespace wosh {
 namespace persistence {
  namespace qt {
	class EncoderXml_Qt_Security_SecurityToken3rdParty;
  };
 };
 namespace security {


#define _SecurityToken3rdparty_VERSION		0.8399

enum SecurityTokenProvider3rdparty_ChannelType {
	CHANNEL_UNKNOWN		= 0x00, // 0x00000000
	CHANNEL_LOCAL		= 0x01, // 0x00000001
	CHANNEL_NETWORK		= 0x02, // 0x00000010
	CHANNEL_TCP			= 0x06, // 0x00000110
	CHANNEL_SSL			= 0x0E, // 0x00001110

	CHANNEL_TELEPHONY	= 0x10  // 0x00010000
 };

class SecurityToken3rdparty_Data {
	public:
		SecurityToken3rdparty_Data() : type(CHANNEL_UNKNOWN) { }
		SecurityToken3rdparty_Data(const SecurityToken3rdparty_Data& m) {
			*this = m;
		 }
		~SecurityToken3rdparty_Data() { }

	public:
		SecurityToken3rdparty_Data& operator=(const SecurityToken3rdparty_Data& m) {
			if (this == &m) return *this; // Same object?
			this->authority_name = m.authority_name;
			this->type = static_cast<SecurityTokenProvider3rdparty_ChannelType>(m.type);
			return *this;
		 }

	public:
		std::string authority_name;
		int type;

 };

class SecurityTokenProvider3rdparty;
class SecurityTokenAuthenticator3rdparty;

class SecurityToken3rdparty : public SecurityToken
 {
	WOSH_SERIALIZABLE(wosh::security::SecurityToken3rdparty)

	public:
		SecurityToken3rdparty()
			: SecurityToken() { }

		SecurityToken3rdparty( const SecurityToken3rdparty& m )
			: SecurityToken(m), data(m.data), user_contact_id(m.user_contact_id) { }

		virtual ~SecurityToken3rdparty() { }

	public:
		inline const std::string& getUserId() const			{ return this->user_contact_id; }
		inline const std::string& getAuthorityName() const	{ return this->data.authority_name; }

		inline SecurityTokenProvider3rdparty_ChannelType getAuthorityChannel() const {
			return static_cast<SecurityTokenProvider3rdparty_ChannelType>(this->data.type);
		 }

		inline double getVersion() const					{ return _SecurityToken3rdparty_VERSION; }
		inline SOURCE_ACCESS getSourceAccess() const		{ return SecurityToken::ACCESS_EXTERNAL; }

	protected:
		SecurityToken3rdparty_Data data;
		std::string user_contact_id;

		friend class wosh::security::SecurityTokenProvider3rdparty;
		friend class wosh::security::SecurityTokenAuthenticator3rdparty;
		friend class wosh::persistence::qt::EncoderXml_Qt_Security_SecurityToken3rdParty;

}; // class def


//////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////

class SecurityTokenProvider3rdparty : public SecurityTokenProvider
 {
	WOSH_CLASSNAME(wosh::security::SecurityTokenProvider3rdparty)
	WOSH_CLASS_ABSTRACT(false)
	WOSH_CLONE_CONSTRUCTOR(wosh::security::SecurityTokenProvider3rdparty)

	public:
		SecurityTokenProvider3rdparty();
		SecurityTokenProvider3rdparty( const SecurityTokenProvider3rdparty& m );
		virtual ~SecurityTokenProvider3rdparty() { }

	public:
		inline double getVersion() const		{ return _SecurityToken3rdparty_VERSION; }

	public:
		virtual SecurityToken* signMessage( Message* message, const std::string& username, const std::string& account_name ) const;

	protected:
		virtual SecurityToken* createToken() const;
		virtual SecurityToken3rdparty* createToken( const std::string& account_name ) const;

	public:
		WRESULT setAuthorityName( const std::string& authority_name );
		WRESULT setAuthorityChannel( SecurityTokenProvider3rdparty_ChannelType type );

	public:
		inline const std::string& getAuthorityName() const		{ return this->data.authority_name; }
		inline SecurityTokenProvider3rdparty_ChannelType getAuthorityChannel() const {
			return static_cast<SecurityTokenProvider3rdparty_ChannelType>(this->data.type);
		 }

	protected:
		SecurityToken3rdparty_Data data;

}; // class def


//////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////

class SecurityTokenAuthenticator3rdparty : public SecurityTokenAuthenticator
 {
	WOSH_CLASSNAME(wosh::security::SecurityTokenAuthenticator3rdparty)
	WOSH_CLASS_ABSTRACT(false)
//	WOSH_CLONE_CONSTRUCTOR(wosh::security::SecurityTokenAuthenticator3rdparty)

	public:
		SecurityTokenAuthenticator3rdparty();
	//	SecurityTokenAuthenticator3rdparty( const SecurityTokenAuthenticator3rdparty& m );
		virtual ~SecurityTokenAuthenticator3rdparty() { }

	public:
		bool isSupported( const char* provider_classname, double version = 0.0 ) const;

		inline double getVersion() const		{ return _SecurityToken3rdparty_VERSION; }

	public:
		WRESULT validateToken( const SecurityToken* token ) const;

	protected:
		std::vector<SecurityToken3rdparty_Data> trusted;
		std::vector<SecurityToken3rdparty_Data> denied;


}; // class def
 
 
 }; // namespace security
}; // namespace wosh

#endif //__WOSH_Security_SecurityAuthentication3rdParty_H__
